import useMain from "@/stores/useMain.js";
import { mapState } from 'pinia';
import { Util,RxIconShow } from '@lowcode/jpaas-base-lib';
import createRoutes from "@/views/layout/js/create-routes";
export default {
    mixins:[createRoutes],
    components:{
        RxIconShow
    },
    computed:{
        ...mapState(useMain,['menus']),
        lang(){
            return window.localStorage.getItem('language') || 'zh-CN'
        }
    },
    data(){
        return {
            load:null,
            menuKey: '',
            appId: '',
            menu:[],
            activeKey:''
        }
    },
    methods:{
        getTabIcon(icon){
            let fn = new Function(`return ${icon}`)()
            return fn.icon ;
        },
        getName(name){
            if(Util.getObjType(name) === 'object'){
                return name[this.lang]
            }else if (name.includes("{") && name.includes("}")){
                let fn = new Function(`return ${name}`)();
                return fn[this.lang]
            }
            return name ;
        },
        getSelectMenus(menus, menuKey) {
            let selectMenus = [];
            for (let i = 0; i < menus.length; i++) {
                if (menus[i].menuKey === menuKey) {
                    selectMenus = menus[i].children;
                    break;
                } else {
                    if (menus[i].children && menus[i].children.length > 0) {
                        let res = this.getSelectMenus(menus[i].children, menuKey);
                        if (res && res.length > 0) {
                            selectMenus = res;
                            break;
                        }
                    }
                }
            }
            return selectMenus;
        },
        async init(){
            let {menuKey,appId,appKey} = this.$route.query ;
            if(!appId && !appKey){
                return
            }
            let app = this.menus.find(item=>(item.appId === appId || item.clientCode === appKey))
            this.menuKey = menuKey ;
            this.appId = app.appId ;
            let menus = await this.actionMenus(app.appId,false);
            this.menu = this.getSelectMenus(menus,menuKey);
            if(this.menu[0]){
                this.activeKey = this.menu[0].menuKey ;
            }

        },
        cancelWatch(){
            this.load && this.load()
            this.load = null ;
        }
    },
    created(){
        this.refresh = true ;
        this.init();
        this.load = this.$watch(()=>this.$route,()=>{
            this.init()
        },{deep:true})
    },

    beforeUnmount(){
        this.cancelWatch()
    }
}
